<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. 新增编辑页 ######1). 在WebContent/WEB-INF/jsp/目录下创建editItem.jsp文件123456789101112131415161718192021222324252627282930313233343536373839&amp;lt;%@ page language=&amp;quot;java&amp;quot; contentType=&amp;quot;text/html">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="JakartaEE-Spring、MyBatis和SpringMVC的整合(二)">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/JakartaEE-Framework/JakartaEE-Spring、MyBatis和SpringMVC的整合(二)/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. 新增编辑页 ######1). 在WebContent/WEB-INF/jsp/目录下创建editItem.jsp文件123456789101112131415161718192021222324252627282930313233343536373839&amp;lt;%@ page language=&amp;quot;java&amp;quot; contentType=&amp;quot;text/html">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-fbffc67f5c2a5567.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-d42d4ba19eb0c592?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-e9e52b4b9f2cfda6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-191b96704a54a627.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-29fc96f175140171.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-6c186e288e44b9d4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-c25e67d2d8caf767.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-0f9296ace6c732e5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-354a1ffb2dd17069.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-18daf2b88fd2a959.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T10:50:49.578Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JakartaEE-Spring、MyBatis和SpringMVC的整合(二)">
<meta name="twitter:description" content="####1. 新增编辑页 ######1). 在WebContent/WEB-INF/jsp/目录下创建editItem.jsp文件123456789101112131415161718192021222324252627282930313233343536373839&amp;lt;%@ page language=&amp;quot;java&amp;quot; contentType=&amp;quot;text/html">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-fbffc67f5c2a5567.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/JakartaEE-Framework/JakartaEE-Spring、MyBatis和SpringMVC的整合(二)/"/>





  <title>JakartaEE-Spring、MyBatis和SpringMVC的整合(二) | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/JakartaEE-Framework/JakartaEE-Spring、MyBatis和SpringMVC的整合(二)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">JakartaEE-Spring、MyBatis和SpringMVC的整合(二)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. 新增编辑页</p>
<p>######1). 在WebContent/WEB-INF/jsp/目录下创建editItem.jsp文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot; pageEncoding=&quot;UTF-8&quot;%&gt;</span><br><span class="line">&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot;%&gt;</span><br><span class="line">&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/fmt&quot; prefix=&quot;fmt&quot; %&gt;</span><br><span class="line">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">	&lt;head&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;</span><br><span class="line">		&lt;title&gt;修改商品信息&lt;/title&gt;</span><br><span class="line">	&lt;/head&gt;</span><br><span class="line">	&lt;body&gt;</span><br><span class="line">		&lt;!-- 上传图片是需要指定属性 enctype=&quot;multipart/form-data&quot; --&gt;</span><br><span class="line">	    &lt;!-- &lt;form id=&quot;itemForm&quot; action=&quot;&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt; --&gt;</span><br><span class="line">	    &lt;form id=&quot;itemForm&quot; action=&quot;$&#123;pageContext.request.contextPath &#125;/updateItem.action&quot; method=&quot;post&quot;&gt;</span><br><span class="line">	    	&lt;input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;$&#123;item.id &#125;&quot;/&gt;修改商品信息：</span><br><span class="line">	    	&lt;table width=&quot;100%&quot; border=&quot;1&quot;&gt;</span><br><span class="line">	    		&lt;tr&gt;</span><br><span class="line">	    			&lt;td&gt;商品名称&lt;/td&gt;</span><br><span class="line">	    			&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;$&#123;item.name &#125;&quot;/&gt;&lt;/td&gt;</span><br><span class="line">	    		&lt;/tr&gt;</span><br><span class="line">	    		&lt;tr&gt;</span><br><span class="line">	    			&lt;td&gt;商品价格&lt;/td&gt;</span><br><span class="line">	    			&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;price&quot; value=&quot;$&#123;item.price &#125;&quot;/&gt;&lt;/td&gt;</span><br><span class="line">	    		&lt;/tr&gt;</span><br><span class="line">	    		&lt;tr&gt;</span><br><span class="line">	    			&lt;td&gt;商品简介&lt;/td&gt;</span><br><span class="line">	    			&lt;td&gt;&lt;textarea rows=&quot;3&quot; cols=&quot;30&quot; name=&quot;detail&quot;&gt;$&#123;item.detail&#125;&lt;/textarea&gt;&lt;/td&gt;</span><br><span class="line">	    		&lt;/tr&gt;</span><br><span class="line">	    		&lt;tr&gt;</span><br><span class="line">	    			&lt;td colspan=&quot;2&quot; align=&quot;center&quot;&gt;</span><br><span class="line">	    				&lt;input type=&quot;submit&quot; value=&quot;提交&quot;/&gt;</span><br><span class="line">	    			&lt;/td&gt;</span><br><span class="line">	    		&lt;/tr&gt;</span><br><span class="line">	    	&lt;/table&gt;</span><br><span class="line">	    </span><br><span class="line">	    &lt;/form&gt;</span><br><span class="line">	</span><br><span class="line">		</span><br><span class="line">	&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure></p>
<a id="more"></a>
<p>######2). Item类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line">public class Item &#123;</span><br><span class="line">	// 编号</span><br><span class="line">	private Integer id;</span><br><span class="line">	// 商品名称</span><br><span class="line">	private String name;</span><br><span class="line">	// 价格</span><br><span class="line">	private Float price;</span><br><span class="line">	// 图片</span><br><span class="line">	private String pic;</span><br><span class="line">	// 描述</span><br><span class="line">	private String detail;</span><br><span class="line">	// 时间戳</span><br><span class="line">	private Timestamp createtime;</span><br><span class="line"></span><br><span class="line">	public Integer getId() &#123;</span><br><span class="line">		return id;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setId(Integer id) &#123;</span><br><span class="line">		this.id = id;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public String getName() &#123;</span><br><span class="line">		return name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setName(String name) &#123;</span><br><span class="line">		this.name = name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public Float getPrice() &#123;</span><br><span class="line">		return price;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setPrice(Float price) &#123;</span><br><span class="line">		this.price = price;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public String getPic() &#123;</span><br><span class="line">		return pic;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setPic(String pic) &#123;</span><br><span class="line">		this.pic = pic;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public Timestamp getCreatetime() &#123;</span><br><span class="line">		return createtime;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setCreatetime(Timestamp createtime) &#123;</span><br><span class="line">		this.createtime = createtime;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public String getDetail() &#123;</span><br><span class="line">		return detail;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setDetail(String detail) &#123;</span><br><span class="line">		this.detail = detail;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;Item [id=&quot; + id + &quot;, name=&quot; + name + &quot;, price=&quot; + price + &quot;, pic=&quot; + pic + &quot;, detail=&quot; + detail</span><br><span class="line">				+ &quot;, createtime=&quot; + createtime + &quot;]&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######3). 在ItemController类中添加方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">// 标记为控制器</span><br><span class="line">@Controller</span><br><span class="line">public class ItemController &#123;</span><br><span class="line">	</span><br><span class="line">	// 注入Service</span><br><span class="line">	@Autowired </span><br><span class="line">	ItemService service;</span><br><span class="line">	</span><br><span class="line">	// 设置网络访问路径/itemEdit.action</span><br><span class="line">	@RequestMapping(&quot;itemEdit&quot;)</span><br><span class="line">	public ModelAndView editItem(HttpServletRequest request) &#123;</span><br><span class="line">		// 从request中取出参数</span><br><span class="line">		String strId = request.getParameter(&quot;id&quot;);</span><br><span class="line">		int id = Integer.valueOf(strId);</span><br><span class="line">		// 调用服务</span><br><span class="line">		Item item = service.getItemById(id);</span><br><span class="line">		// 把结果传递给页面</span><br><span class="line">		ModelAndView modelAndView = new ModelAndView();</span><br><span class="line">		modelAndView.addObject(&quot;item&quot;, item);</span><br><span class="line">		// 设置逻辑视图</span><br><span class="line">		modelAndView.setViewName(&quot;editItem&quot;);</span><br><span class="line">		return modelAndView;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######4). 在ItemService中创建getItemById方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">public interface ItemService &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * 获取商品信息</span><br><span class="line">	 * @param id id</span><br><span class="line">	 * @return</span><br><span class="line">	 */</span><br><span class="line">	Item getItemById(int id);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######5). 在ItemServiceImpl类中实现getItemById方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">// 标记为Service</span><br><span class="line">@Service</span><br><span class="line">public class ItemServiceImpl implements ItemService &#123;</span><br><span class="line"></span><br><span class="line">	// 注入ItemMapper</span><br><span class="line">	@Autowired</span><br><span class="line">	ItemMapper mapper;</span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	public Item getItemById(int id) &#123;</span><br><span class="line">		return mapper.getItemId(id);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######6). 在ItemMapper接口中创建getItemId方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">public interface ItemMapper &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * 根据id获取商品信息</span><br><span class="line">	 * @param id id</span><br><span class="line">	 * @return</span><br><span class="line">	 */</span><br><span class="line">	Item getItemId(int id);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######7). 在ItemMapper.xml文件中添加查询语句<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;mapper namespace=&quot;com.mazaiting.mapper.ItemMapper&quot;&gt;</span><br><span class="line">	&lt;select id=&quot;getItemId&quot; parameterType=&quot;int&quot; resultType=&quot;Item&quot;&gt;</span><br><span class="line">		select * from items where id = #&#123;id&#125; </span><br><span class="line">	&lt;/select&gt;</span><br><span class="line">&lt;/mapper&gt;</span><br></pre></td></tr></table></figure></p>
<p>######8). 在test源码包下添加测试方法进行测试</p>
<ul>
<li><p><strong>ItemMapperTest.java</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">public class ItemMapperTest &#123;</span><br><span class="line">	ApplicationContext context;</span><br><span class="line">	ItemMapper mapper;</span><br><span class="line">	@Before</span><br><span class="line">	public void init()&#123;</span><br><span class="line">		context = new ClassPathXmlApplicationContext(Constant.PATHS);</span><br><span class="line">		mapper = context.getBean(ItemMapper.class);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testGetItemId()&#123;</span><br><span class="line">		Item item = mapper.getItemId(1);</span><br><span class="line">		System.out.println(item.toString());</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>ItemServiceTest</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">public class ItemServcieTest &#123;</span><br><span class="line">	ApplicationContext context;</span><br><span class="line">	ItemService service;</span><br><span class="line">	@Before</span><br><span class="line">	public void init() &#123;</span><br><span class="line">		context = new ClassPathXmlApplicationContext(Constant.PATHS);</span><br><span class="line">		service = context.getBean(ItemService.class);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test </span><br><span class="line">	public void testGetItemById()&#123;</span><br><span class="line">		Item item = service.getItemById(1);</span><br><span class="line">		System.out.println(item.toString());</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>######9). 运行项目测试(请在以上测试代码成功打印数据之后进行项目整体的测试，否则浪费时间)<br><img src="https://upload-images.jianshu.io/upload_images/3110861-fbffc67f5c2a5567.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"></p>
<p>####2.SpringMVC框架默认支持的参数类型<br>处理器形参中添加如下类型的参数，处理适配器会默认识别并进行赋值。</p>
<ul>
<li>HttpServletRequest：通过request对象获取请求信息。</li>
<li>HttpServletResponse：通过response处理响应信息。</li>
<li>HttpSession：通过session对象得到session中存放的对象。</li>
<li>Model/ModelMap：ModelMap是Model接口的实现类，我们可通过Model或ModelMap向页面传递数据<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Items items = itemService.getItemById(id);</span><br><span class="line">model.addAttribute(&quot;item&quot;, items);</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>页面中通过${item.XXXX}获取item对象的属性值。 </p>
<p>####3. 简单数据类型绑定<br>将editItem方法修改如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">@RequestMapping(&quot;/itemEdit&quot;)</span><br><span class="line">public String editItem(Integer id,Model model) &#123;</span><br><span class="line">	// 调用服务</span><br><span class="line">	Item item = service.getItemById(id);</span><br><span class="line">	// 把数据传递给页面，需要用到Model接口</span><br><span class="line">	model.addAttribute(&quot;item&quot;, item);</span><br><span class="line">	return &quot;editItem&quot;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>####4. 支持的数据类型<br>SpringMVC框架支持的数据类型有：</p>
<ul>
<li>整形：Integer、int</li>
<li>字符串：String</li>
<li>单精度：Float、float</li>
<li>双精度：Double、double</li>
<li>布尔型：Boolean、boolean<br>说明：对于布尔类型的参数，请求的参数值为true或false。处理器方法可是这样的：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public String editItem(Model model,Integer id,Boolean status) throws Exception &#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>至于请求的url，可是<code>http://localhost:8080/xxx.action?id=2&amp;status=false</code></p>
<p><strong>注意：参数类型推荐使用包装数据类型，因为基础数据类型不可以为null。</strong></p>
<p>####5. @RequestParam</p>
<p>使用@RequestParam注解常用于处理简单类型的绑定。</p>
<ul>
<li>value：参数名字，即入参的请求参数名字，如value=“item_id”表示请求的参数区中的名字为item_id的参数的值将传入。</li>
<li><p>required：是否必须，默认是true，表示请求中一定要有相应的参数，否则将报如下错误：<br>  <img src="http://upload-images.jianshu.io/upload_images/3110861-d42d4ba19eb0c592?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图2.png"></p>
</li>
<li><p>defaultValue：默认值，表示如果请求中没有同名参数时的默认值。<br>修改方法为：</p>
<figure class="highlight plain"><figcaption><span>value表示链接中的参数名，这里必须和连接中的相同</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">// defaultValue为默认值</span><br><span class="line">// required表示此参数不能为空</span><br><span class="line">@RequestMapping(&quot;/itemEdit&quot;)</span><br><span class="line">public String editItem(@RequestParam(value=&quot;id&quot;,defaultValue=&quot;1&quot;,required=true) Integer ids,Model model) &#123;</span><br><span class="line">	// 调用服务</span><br><span class="line">	Item item = service.getItemById(ids);</span><br><span class="line">	// 把数据传递给页面，需要用到Model接口</span><br><span class="line">	model.addAttribute(&quot;item&quot;, item);</span><br><span class="line">	return &quot;editItem&quot;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>####6. 绑定pojo类型</p>
<p>######1). 在ItemController中创建<code>/updateItem.action</code>处理方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">// 标记为控制器</span><br><span class="line">@Controller</span><br><span class="line">public class ItemController &#123;</span><br><span class="line">	</span><br><span class="line">	// 注入Service</span><br><span class="line">	@Autowired </span><br><span class="line">	ItemService service;</span><br><span class="line">	</span><br><span class="line">	@RequestMapping(&quot;/updateItem&quot;)</span><br><span class="line">	public String updateItem(Item item) &#123;</span><br><span class="line">		service.updateItem(item);</span><br><span class="line">		// 返回成功页面</span><br><span class="line">		return &quot;success&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######2). 在ItemService中创建方法，并在ItemServiceImple中实现该方法</p>
<ul>
<li><p><strong>ItemService</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">public interface ItemService &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * 更新条目</span><br><span class="line">	 * @param item Java Bean</span><br><span class="line">	 */</span><br><span class="line">	void updateItem(Item item);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>ItemServiceImpl</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">// 标记为Service</span><br><span class="line">@Service</span><br><span class="line">public class ItemServiceImpl implements ItemService &#123;</span><br><span class="line"></span><br><span class="line">	// 注入ItemMapper</span><br><span class="line">	@Autowired</span><br><span class="line">	ItemMapper mapper;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public void updateItem(Item item) &#123;</span><br><span class="line">		mapper.updateItem(item);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>######3). 在ItemMapper接口中添加方法并在ItemMapper.xml文件中添加查询语句</p>
<ul>
<li><p><strong>ItemMapper</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">public interface ItemMapper &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * 更新数据</span><br><span class="line">	 * @param item</span><br><span class="line">	 * @return</span><br><span class="line">	 */</span><br><span class="line">	void updateItem(Item item);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>ItemMapper.xml</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;mapper namespace=&quot;com.mazaiting.mapper.ItemMapper&quot;&gt;</span><br><span class="line">	&lt;update id=&quot;updateItem&quot; parameterType=&quot;Item&quot;&gt;</span><br><span class="line">		update items set name=#&#123;name&#125;, price=#&#123;price&#125;, detail=#&#123;detail&#125; where id = #&#123;id&#125;</span><br><span class="line">	&lt;/update&gt;</span><br><span class="line">&lt;/mapper&gt;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>######4). 编写测试代码</p>
<ul>
<li><p><strong>ItemMapperTest</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">public class ItemMapperTest &#123;</span><br><span class="line">	ApplicationContext context;</span><br><span class="line">	ItemMapper mapper;</span><br><span class="line">	@Before</span><br><span class="line">	public void init()&#123;</span><br><span class="line">		context = new ClassPathXmlApplicationContext(Constant.PATHS);</span><br><span class="line">		mapper = context.getBean(ItemMapper.class);</span><br><span class="line">	&#125;</span><br><span class="line">		</span><br><span class="line">	@Test</span><br><span class="line">	public void testUpdateItem()&#123;</span><br><span class="line">		Item item = new Item();</span><br><span class="line">		item.setId(1);</span><br><span class="line">		item.setName(&quot;小米&quot;);</span><br><span class="line">		item.setDetail(&quot;智能手机&quot;);</span><br><span class="line">		item.setPrice(200.0f);</span><br><span class="line">		mapper.updateItem(item);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>ItemServiceTest</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">public class ItemServcieTest &#123;</span><br><span class="line">	ApplicationContext context;</span><br><span class="line">	ItemService service;</span><br><span class="line">	@Before</span><br><span class="line">	public void init() &#123;</span><br><span class="line">		context = new ClassPathXmlApplicationContext(Constant.PATHS);</span><br><span class="line">		service = context.getBean(ItemService.class);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testUpdateItem()&#123;</span><br><span class="line">		Item item = new Item();</span><br><span class="line">		item.setId(1);</span><br><span class="line">		item.setName(&quot;小米4c&quot;);</span><br><span class="line">		item.setDetail(&quot;智能手机&quot;);</span><br><span class="line">		item.setPrice(200.0f);</span><br><span class="line">		service.updateItem(item);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>######5). 创建success.jsp文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;</span><br><span class="line">    pageEncoding=&quot;UTF-8&quot;%&gt;</span><br><span class="line">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">	&lt;head&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;</span><br><span class="line">		&lt;title&gt;Success&lt;/title&gt;</span><br><span class="line">	&lt;/head&gt;</span><br><span class="line">	&lt;body&gt;</span><br><span class="line">		&lt;h1&gt;更新成功&lt;/h1&gt;</span><br><span class="line">	&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure></p>
<p>######6). 测试<br><img src="https://upload-images.jianshu.io/upload_images/3110861-e9e52b4b9f2cfda6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图3.png"><br>修改商品简介后点击提交<br><img src="https://upload-images.jianshu.io/upload_images/3110861-191b96704a54a627.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图4.png"><br>访问<code>http://localhost:8080/Ssm/itemList.action</code><br><img src="https://upload-images.jianshu.io/upload_images/3110861-29fc96f175140171.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图5.png"></p>
<p>####7. 自定义参数绑定</p>
<p>######1). 修改editItem.jsp如下<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">  &lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot; pageEncoding=&quot;UTF-8&quot;%&gt;</span><br><span class="line">&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot;%&gt;</span><br><span class="line">&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/fmt&quot; prefix=&quot;fmt&quot; %&gt;</span><br><span class="line">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">	&lt;head&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;</span><br><span class="line">		&lt;title&gt;修改商品信息&lt;/title&gt;</span><br><span class="line">	&lt;/head&gt;</span><br><span class="line">	&lt;body&gt;</span><br><span class="line">		&lt;!-- 上传图片是需要指定属性 enctype=&quot;multipart/form-data&quot; --&gt;</span><br><span class="line">	    &lt;!-- &lt;form id=&quot;itemForm&quot; action=&quot;&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt; --&gt;</span><br><span class="line">	    &lt;form id=&quot;itemForm&quot; action=&quot;$&#123;pageContext.request.contextPath &#125;/updateItem.action&quot; method=&quot;post&quot;&gt;</span><br><span class="line">	    	&lt;input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;$&#123;item.id &#125;&quot;/&gt;修改商品信息：</span><br><span class="line">	    	&lt;table width=&quot;100%&quot; border=&quot;1&quot;&gt;</span><br><span class="line">	    		&lt;tr&gt;</span><br><span class="line">	    			&lt;td&gt;商品名称&lt;/td&gt;</span><br><span class="line">	    			&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;$&#123;item.name &#125;&quot;/&gt;&lt;/td&gt;</span><br><span class="line">	    		&lt;/tr&gt;</span><br><span class="line">	    		&lt;tr&gt;</span><br><span class="line">	    			&lt;td&gt;商品价格&lt;/td&gt;</span><br><span class="line">	    			&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;price&quot; value=&quot;$&#123;item.price &#125;&quot;/&gt;&lt;/td&gt;</span><br><span class="line">	    		&lt;/tr&gt;</span><br><span class="line">	    		&lt;tr&gt;</span><br><span class="line">	    			&lt;td&gt;商品生产日期&lt;/td&gt;</span><br><span class="line">	    			&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;createtime&quot; </span><br><span class="line">	    			value=&quot;&lt;fmt:formatDate value=&quot;$&#123;item.createtime&#125;&quot; pattern=&quot;yyyy-MM-dd HH:mm:ss&quot;/&gt;&quot;/&gt;</span><br><span class="line">	    			&lt;/td&gt;</span><br><span class="line">	    		&lt;/tr&gt;</span><br><span class="line">	    		&lt;tr&gt;</span><br><span class="line">	    			&lt;td&gt;商品简介&lt;/td&gt;</span><br><span class="line">	    			&lt;td&gt;&lt;textarea rows=&quot;3&quot; cols=&quot;30&quot; name=&quot;detail&quot;&gt;$&#123;item.detail &#125;&lt;/textarea&gt;&lt;/td&gt;</span><br><span class="line">	    		&lt;/tr&gt;</span><br><span class="line">	    		&lt;tr&gt;</span><br><span class="line">	    			&lt;td colspan=&quot;2&quot; align=&quot;center&quot;&gt;</span><br><span class="line">	    				&lt;input type=&quot;submit&quot; value=&quot;提交&quot;/&gt;</span><br><span class="line">	    			&lt;/td&gt;</span><br><span class="line">	    		&lt;/tr&gt;</span><br><span class="line">	    	&lt;/table&gt;</span><br><span class="line">	    </span><br><span class="line">	    &lt;/form&gt;</span><br><span class="line">	</span><br><span class="line">		</span><br><span class="line">	&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-6c186e288e44b9d4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图6.png"></p>
<p>######2). 此时修改时间提交，会出现400的异常<br><img src="https://upload-images.jianshu.io/upload_images/3110861-c25e67d2d8caf767.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图7.png"></p>
<p>######3). 自定义Converter<br>在com.mazaiting.convert包下创建DateConverter类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * SpringMVC转换器</span><br><span class="line"> * Converter&lt;S, T&gt;  S：source源数据类型，T：target目标数据类型</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class DateConverter implements Converter&lt;String, Date&gt;&#123;</span><br><span class="line"></span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	public Date convert(String source) &#123;</span><br><span class="line">		SimpleDateFormat formatter = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;, Locale.getDefault());</span><br><span class="line">		try &#123;</span><br><span class="line">			Date date = formatter.parse(source);</span><br><span class="line">			return date;</span><br><span class="line">		&#125; catch (ParseException e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;</span><br><span class="line">		return null;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######4). 配置Converter<br>在springmvc.xml配置文件中添加如下配置:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;</span><br><span class="line">    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; </span><br><span class="line">    xmlns:p=&quot;http://www.springframework.org/schema/p&quot;</span><br><span class="line">    xmlns:context=&quot;http://www.springframework.org/schema/context&quot;</span><br><span class="line">    xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;</span><br><span class="line">    xsi:schemaLocation=&quot;</span><br><span class="line">        http://www.springframework.org/schema/beans </span><br><span class="line">        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd</span><br><span class="line">        http://www.springframework.org/schema/mvc </span><br><span class="line">        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd</span><br><span class="line">        http://www.springframework.org/schema/context </span><br><span class="line">        http://www.springframework.org/schema/context/spring-context-4.0.xsd&quot;&gt;</span><br><span class="line"></span><br><span class="line">	&lt;!-- 配置注解驱动，如果配置此标签，则不用配置处理器映射器和处理器适配器 --&gt;</span><br><span class="line">	&lt;mvc:annotation-driven conversion-service=&quot;conversionService&quot;/&gt;</span><br><span class="line">	</span><br><span class="line">	&lt;!-- 转换器配置 --&gt;</span><br><span class="line">	&lt;bean id=&quot;conversionService&quot; class=&quot;org.springframework.format.support.FormattingConversionServiceFactoryBean&quot;&gt;</span><br><span class="line">		&lt;property name=&quot;converters&quot;&gt;</span><br><span class="line">			&lt;set&gt;</span><br><span class="line">				&lt;bean class=&quot;com.mazaiting.converter.DateConverter&quot;/&gt;</span><br><span class="line">			&lt;/set&gt;</span><br><span class="line">		&lt;/property&gt;</span><br><span class="line">	&lt;/bean&gt;</span><br><span class="line">	</span><br><span class="line">&lt;/beans&gt;</span><br></pre></td></tr></table></figure></p>
<p>######5). 测试点击页面之后不再发生错误。虽然提交成功，但时间查询后发现没有改变，原因是我们在更新数据的时候并没有更新时间。</p>
<p>####8. 绑定数组</p>
<p>######1). 修改itemList.jsp<br>在每个条目之前增加一个checkbox。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;</span><br><span class="line">	pageEncoding=&quot;UTF-8&quot;%&gt;</span><br><span class="line">&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot;%&gt;</span><br><span class="line">&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/fmt&quot; prefix=&quot;fmt&quot;%&gt;</span><br><span class="line">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">	&lt;head&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;</span><br><span class="line">		&lt;title&gt;查询商品列表&lt;/title&gt;</span><br><span class="line">	&lt;/head&gt;</span><br><span class="line">	&lt;body&gt;</span><br><span class="line">		&lt;form action=&quot;$&#123;pageContext.request.contextPath &#125;/item/queryitem.action&quot; method=&quot;post&quot;&gt;</span><br><span class="line">			查询条件：</span><br><span class="line">			&lt;table width=&quot;100%&quot; border=&quot;1&quot;&gt;</span><br><span class="line">				&lt;tr&gt;</span><br><span class="line">					&lt;td&gt;商品id：&lt;input type=&quot;text&quot; name=&quot;item.id&quot; /&gt;&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;商品名称：&lt;input type=&quot;text&quot; name=&quot;item.name&quot; /&gt;&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;&lt;input type=&quot;submit&quot; value=&quot;查询&quot;/&gt;&lt;/td&gt;</span><br><span class="line">				&lt;/tr&gt;</span><br><span class="line">			&lt;/table&gt;</span><br><span class="line">			商品列表：</span><br><span class="line">			&lt;table width=&quot;100%&quot; border=&quot;1&quot;&gt;</span><br><span class="line">				&lt;tr&gt;</span><br><span class="line">					&lt;td&gt;选择&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;商品名称&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;商品价格&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;生产日期&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;商品描述&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;操作&lt;/td&gt;</span><br><span class="line">				&lt;/tr&gt;</span><br><span class="line">				&lt;c:forEach items=&quot;$&#123;itemList &#125;&quot; var=&quot;item&quot;&gt;</span><br><span class="line">					&lt;tr&gt;</span><br><span class="line">						&lt;td&gt;&lt;input type=&quot;checkbox&quot; name=&quot;ids&quot; value=&quot;$&#123;item.id &#125;&quot;/&gt;&lt;/td&gt;</span><br><span class="line">						&lt;td&gt;$&#123;item.name &#125;&lt;/td&gt;</span><br><span class="line">						&lt;td&gt;$&#123;item.price &#125;&lt;/td&gt;</span><br><span class="line">						&lt;td&gt;&lt;fmt:formatDate value=&quot;$&#123;item.createtime &#125;&quot; pattern=&quot;yyyy-MM-dd HH:mm:ss&quot;/&gt;&lt;/td&gt;</span><br><span class="line">						&lt;td&gt;$&#123;item.detail &#125;&lt;/td&gt;</span><br><span class="line">						&lt;td&gt;&lt;a href=&quot;$&#123;pageContext.request.contextPath &#125;/itemEdit.action?id=$&#123;item.id&#125;&quot;&gt;修改&lt;/a&gt;&lt;/td&gt;</span><br><span class="line">					&lt;/tr&gt;</span><br><span class="line">				&lt;/c:forEach&gt;</span><br><span class="line">			&lt;/table&gt;</span><br><span class="line">		&lt;/form&gt;</span><br><span class="line">	&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-0f9296ace6c732e5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图8.png"></p>
<p>######2). 创建包装类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">public class QueryVo &#123;</span><br><span class="line">	private Item item;</span><br><span class="line">	private String[] ids;</span><br><span class="line">	public Item getItem() &#123;</span><br><span class="line">		return item;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setItem(Item item) &#123;</span><br><span class="line">		this.item = item;</span><br><span class="line">	&#125;</span><br><span class="line">	public String[] getIds() &#123;</span><br><span class="line">		return ids;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setIds(String[] ids) &#123;</span><br><span class="line">		this.ids = ids;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######3). ItemController类中添加方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">@RequestMapping(&quot;item/queryitem&quot;)</span><br><span class="line">public String queryitem(QueryVo queryVo, String[] ids)&#123;</span><br><span class="line">	// 打印绑定结果</span><br><span class="line">	System.out.println(ids.toString());</span><br><span class="line">	System.out.println(queryVo.getIds().toString());</span><br><span class="line">	System.out.println(queryVo.getItem().toString());</span><br><span class="line">	return &quot;success&quot;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>####9.   表单的数据绑定到List</p>
<p>######1). 修改QueryVo类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">public class QueryVo &#123;</span><br><span class="line">	private Item item;</span><br><span class="line">	private String[] ids;</span><br><span class="line">	private List&lt;Item&gt; itemList;</span><br><span class="line">	public Item getItem() &#123;</span><br><span class="line">		return item;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setItem(Item item) &#123;</span><br><span class="line">		this.item = item;</span><br><span class="line">	&#125;</span><br><span class="line">	public String[] getIds() &#123;</span><br><span class="line">		return ids;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setIds(String[] ids) &#123;</span><br><span class="line">		this.ids = ids;</span><br><span class="line">	&#125;</span><br><span class="line">	public List&lt;Item&gt; getItemList() &#123;</span><br><span class="line">		return itemList;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setItemList(List&lt;Item&gt; itemList) &#123;</span><br><span class="line">		this.itemList = itemList;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######2). 修改itemList.jsp<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;</span><br><span class="line">	pageEncoding=&quot;UTF-8&quot;%&gt;</span><br><span class="line">&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot;%&gt;</span><br><span class="line">&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/fmt&quot; prefix=&quot;fmt&quot;%&gt;</span><br><span class="line">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">	&lt;head&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;</span><br><span class="line">		&lt;title&gt;查询商品列表&lt;/title&gt;</span><br><span class="line">	&lt;/head&gt;</span><br><span class="line">	&lt;body&gt;</span><br><span class="line">		&lt;form action=&quot;$&#123;pageContext.request.contextPath &#125;/item/queryitem.action&quot; method=&quot;post&quot;&gt;</span><br><span class="line">			查询条件：</span><br><span class="line">			&lt;table width=&quot;100%&quot; border=&quot;1&quot;&gt;</span><br><span class="line">				&lt;tr&gt;</span><br><span class="line">					&lt;td&gt;商品id：&lt;input type=&quot;text&quot; name=&quot;item.id&quot; /&gt;&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;商品名称：&lt;input type=&quot;text&quot; name=&quot;item.name&quot; /&gt;&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;&lt;input type=&quot;submit&quot; value=&quot;查询&quot;/&gt;&lt;/td&gt;</span><br><span class="line">				&lt;/tr&gt;</span><br><span class="line">			&lt;/table&gt;</span><br><span class="line">			商品列表：</span><br><span class="line">			&lt;table width=&quot;100%&quot; border=&quot;1&quot;&gt;</span><br><span class="line">				&lt;tr&gt;</span><br><span class="line">					&lt;td&gt;选择&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;商品名称&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;商品价格&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;生产日期&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;商品描述&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;操作&lt;/td&gt;</span><br><span class="line">				&lt;/tr&gt;</span><br><span class="line">				&lt;c:forEach items=&quot;$&#123;itemList &#125;&quot; var=&quot;item&quot; varStatus=&quot;s&quot;&gt;</span><br><span class="line">					&lt;%-- &lt;tr&gt;</span><br><span class="line">						&lt;td&gt;&lt;input type=&quot;checkbox&quot; name=&quot;ids&quot; value=&quot;$&#123;item.id &#125;&quot;/&gt;&lt;/td&gt;</span><br><span class="line">						&lt;td&gt;$&#123;item.name &#125;&lt;/td&gt;</span><br><span class="line">						&lt;td&gt;$&#123;item.price &#125;&lt;/td&gt;</span><br><span class="line">						&lt;td&gt;&lt;fmt:formatDate value=&quot;$&#123;item.createtime &#125;&quot; pattern=&quot;yyyy-MM-dd HH:mm:ss&quot;/&gt;&lt;/td&gt;</span><br><span class="line">						&lt;td&gt;$&#123;item.detail &#125;&lt;/td&gt;</span><br><span class="line">						&lt;td&gt;&lt;a href=&quot;$&#123;pageContext.request.contextPath &#125;/itemEdit.action?id=$&#123;item.id&#125;&quot;&gt;修改&lt;/a&gt;&lt;/td&gt;</span><br><span class="line">					&lt;/tr&gt; --%&gt;</span><br><span class="line">					&lt;td&gt;&lt;input type=&quot;checkbox&quot; name=&quot;ids&quot; value=&quot;$&#123;item.id &#125;&quot;/&gt;&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;</span><br><span class="line">						&lt;input type=&quot;hidden&quot; name=&quot;itemList[$&#123;s.index &#125;].id&quot; value=&quot;$&#123;item.id &#125;&quot;/&gt;</span><br><span class="line">						&lt;input type=&quot;text&quot; name=&quot;itemList[$&#123;s.index &#125;].name&quot; value=&quot;$&#123;item.name &#125;&quot;/&gt;</span><br><span class="line">					&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;itemList[$&#123;s.index &#125;].price&quot; value=&quot;$&#123;item.price &#125;&quot;/&gt;&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;itemList[$&#123;s.index &#125;].createtime&quot; value=&apos;&lt;fmt:formatDate value=&quot;$&#123;item.createtime &#125;&quot; pattern=&quot;yyyy-MM-dd HH:mm:ss&quot;/&gt;&apos;/&gt;&lt;/td&gt;</span><br><span class="line">					&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;itemList[$&#123;s.index &#125;].detail&quot; value=&quot;$&#123;item.detail &#125;&quot;/&gt;&lt;/td&gt;</span><br><span class="line">				&lt;/c:forEach&gt;</span><br><span class="line">			&lt;/table&gt;</span><br><span class="line">		&lt;/form&gt;</span><br><span class="line">	&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure></p>
<p>&lt;c:forEach&gt;标签中的varStatus属性常用参数总结如下：</p>
<ul>
<li>${status.index}：输出行号，从0开始。</li>
<li>${status.count}：输出行号，从1开始。</li>
<li>${status.current}：当前这次迭代的（集合中的）项。</li>
<li>${status.first}：判断当前项是否为集合中的第一项，返回值为true或false。</li>
<li>${status.last}：判断当前项是否为集合中的最后一项，返回值为true或false。</li>
<li><p>begin、end、step分别表示：起始序号，结束序号，跳跃步伐。<br>####10. other<br>######1). @RequestMapping注解的使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@RequestMapping(value=&quot;/updateitem&quot;,method=&#123;RequestMethod.GET&#125;)</span><br></pre></td></tr></table></figure>
</li>
<li><p>value：指定访问路经</p>
</li>
<li>method：限定请求方法<br>######2). request转向页面<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">// 如果使用原始的方式做页面跳转，必须给的是jsp的完整路径</span><br><span class="line">    request.getRequestDispatcher(&quot;/WEB-INF/jsp/itemList.jsp&quot;).forward(request, response);</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>######3). response重定向<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">response.sendRedirect(&quot;url&quot;)</span><br></pre></td></tr></table></figure></p>
<p>######4). 返回响应结果<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">response.setCharacterEncoding(&quot;utf-8&quot;);</span><br><span class="line">response.setContentType(&quot;application/json;charset=utf-8&quot;);</span><br><span class="line">response.getWriter().write(&quot;json串&quot;);</span><br></pre></td></tr></table></figure></p>
<p>######5). Controller类中重定向<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">@RequestMapping(&quot;/updateItem&quot;)</span><br><span class="line">public String updateItem(Item item) &#123;</span><br><span class="line">	service.updateItem(item);</span><br><span class="line">	// 重定向</span><br><span class="line">	return &quot;redirect:/itemList.action&quot;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######6). Controller类forward转发<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@RequestMapping(value=&quot;/updateitem&quot;,method=&#123;RequestMethod.POST,RequestMethod.GET&#125;)</span><br><span class="line">public String updateItems(Items items) throws UnsupportedEncodingException &#123;</span><br><span class="line">    itemService.updateItem(items);</span><br><span class="line">    return &quot;forward:/item/itemList.action&quot;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>####11. 异常处理<br>系统中异常包括两类：预期异常和运行时异常(RuntimeException)，前者通过捕获异常从而获取异常信息，后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。系统的dao、service、controller出现异常都通过throws Exception向上抛出，最后由SpringMVC前端控制器交由异常处理器进行异常处理，SpringMVC提供全局异常处理器（一个系统只有一个异常处理器）进行统一异常处理。<br><img src="https://upload-images.jianshu.io/upload_images/3110861-354a1ffb2dd17069.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图9.png"></p>
<p>######1). 异常处理器<br>全局异常处理器处理思路：</p>
<ul>
<li>解析出异常类型。</li>
<li>如果该异常类型是系统自定义的异常，直接取出异常信息，在错误页面展示。</li>
<li>如果该异常类型不是系统自定义的异常，那么应取出错误的堆栈信息，并记录下来，在错误页面展示。<br>SpringMVC提供一个HandlerExceptionResolver接口，自定义全局异常处理器必须要实现这个接口，所以我们可在com.mazaiting.exception包下编写一个自定义全局异常处理器。<br>######2). 自定义一个异常类–CustomerException <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 自定义异常</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class CustomerException extends Exception&#123;</span><br><span class="line">	private String msg;</span><br><span class="line"></span><br><span class="line">	public CustomerException() &#123;&#125;</span><br><span class="line">	public CustomerException(String msg) &#123;</span><br><span class="line">		this.msg = msg;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public String getMsg() &#123;</span><br><span class="line">		return msg;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public void setMsg(String msg) &#123;</span><br><span class="line">		this.msg = msg;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>######3). 自定义异常处理器<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 全局异常处理</span><br><span class="line"> * @author mazaiting </span><br><span class="line"> */</span><br><span class="line">public class GlobalExceptionResolver implements HandlerExceptionResolver&#123;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,</span><br><span class="line">			Exception exception) &#123;</span><br><span class="line">		// 判断异常的种类</span><br><span class="line">		String msg = null;</span><br><span class="line">		if (exception instanceof CustomerException) &#123;</span><br><span class="line">			// 如果是自定义异常，就从异常里面取出错误消息</span><br><span class="line">			CustomerException customerException = (CustomerException) exception;</span><br><span class="line">			msg = customerException.getMsg();</span><br><span class="line">		&#125; else &#123;</span><br><span class="line">			// 如果是运行时异常，则取出错误的堆栈信息, 向控制台上打印堆栈信息</span><br><span class="line">			exception.printStackTrace();</span><br><span class="line">			StringWriter writer = new StringWriter();</span><br><span class="line">			PrintWriter printWriter = new PrintWriter(writer);</span><br><span class="line">			exception.printStackTrace(printWriter);</span><br><span class="line">			msg = writer.toString();</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		// 写日志，发短信，发邮件。。。</span><br><span class="line">		</span><br><span class="line">		// 返回一个友好的错误页面，并显示错误信息</span><br><span class="line">		ModelAndView modelAndView = new ModelAndView();</span><br><span class="line">		modelAndView.addObject(&quot;msg&quot;, msg);</span><br><span class="line">		modelAndView.setViewName(&quot;error&quot;);</span><br><span class="line">		return modelAndView;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######4). 创建错误页面<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;</span><br><span class="line">    pageEncoding=&quot;UTF-8&quot;%&gt;</span><br><span class="line">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">	&lt;head&gt;</span><br><span class="line">		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;</span><br><span class="line">		&lt;title&gt;错误页面&lt;/title&gt;</span><br><span class="line">	&lt;/head&gt;</span><br><span class="line">	&lt;body&gt;</span><br><span class="line">		&lt;h1&gt;系统发送异常了。。。。。。。。。。&lt;/h1&gt;</span><br><span class="line">		&lt;br&gt;</span><br><span class="line">		&lt;h2&gt;错误消息为：&lt;/h2&gt;</span><br><span class="line">		&lt;br&gt;</span><br><span class="line">		$&#123;msg &#125;	</span><br><span class="line">	&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure></p>
<p>######5). 在springmvc.xml文件中配置全局异常处理器<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 配置全局异常处理器 --&gt;</span><br><span class="line">&lt;bean class=&quot;com.mazaiting.exception.GlobalExceptionResolver&quot;/&gt;</span><br></pre></td></tr></table></figure></p>
<p>######6). 测试<br>修改方法：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">// 设置网络访问路径/itemList.action</span><br><span class="line">@RequestMapping(&quot;/itemList&quot;)</span><br><span class="line">public ModelAndView itemList() throws CustomerException &#123;</span><br><span class="line">	</span><br><span class="line">	if (true) &#123;</span><br><span class="line">		throw new CustomerException(&quot;异常啦&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	List&lt;Item&gt; list = service.getItemList();</span><br><span class="line">	ModelAndView modelAndView = new ModelAndView();</span><br><span class="line">	// 设置数据</span><br><span class="line">	modelAndView.addObject(&quot;itemList&quot;, list);</span><br><span class="line">	// 设置视图名称</span><br><span class="line">	modelAndView.setViewName(&quot;itemList&quot;);</span><br><span class="line">	return modelAndView;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######7). 运行效果<br><img src="https://upload-images.jianshu.io/upload_images/3110861-18daf2b88fd2a959.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图10.png"></p>
<p><a href="https://gitee.com/mazaiting/Ssm" target="_blank" rel="noopener">代码下载–a8bcbb792</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/JakartaEE-Framework/JakartaEE-Spring、MyBatis和SpringMVC的整合(三)/" rel="next" title="JakartaEE-Spring、MyBatis和SpringMVC的整合(三)">
                <i class="fa fa-chevron-left"></i> JakartaEE-Spring、MyBatis和SpringMVC的整合(三)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/JakartaEE-Framework/JakartaEE-Struts2使用/" rel="prev" title="JakartaEE-Struts2使用">
                JakartaEE-Struts2使用 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
